package player.ui.video;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.peer.ComponentPeer;

import javax.swing.SwingUtilities;

import player.PS;
import player.Tools;
import player.mplayer.MPlayerControl;
import player.ui.browse.FloatPanel;

public class VideoCanvas extends Panel{

	private static final long serialVersionUID = -6240973766872912183L;
	VideoCanvas canvas;
	long wid=0;
	FloatPanel FloatPanel;
	Color keyColor=new Color(0x18,0x18,0x18);
	Color bgColor=new Color(250,200,200);
	
	VideoCanvas(){
		super();
		PS.VideoCanvas=this;
		canvas=this;
		setLayout(null);
	//	bgColor=keyColor;
		setBackground(keyColor);
		addMouseWheelListener(new MouseWheelListener(){
			public void mouseWheelMoved(MouseWheelEvent e) {
				if(PS.videoFrame.isFullSereen){
					return;
				}
				boolean expend=true;
				if(e.getWheelRotation()>0){
					expend=false;
				}
				final Rectangle outRec=Tools.getNewOutBound(
						canvas,e.getPoint(),PS.videoRate, expend);
				if(!expend&outRec.width<250){

				}else{
					new Thread(){
						public void run(){
							PS.videoFrame.setBounds(outRec);
							checkLayout();
						}
					}.start();
				}
			}
		});
		addMouseListener(new MouseListener(){

			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount()==2){
					PS.videoFrame.switchFullScreen();
				}
			}

			public void mouseEntered(MouseEvent e) {}

			public void mouseExited(MouseEvent e) {}

			public void mousePressed(MouseEvent e) {}

			public void mouseReleased(MouseEvent e) {}

		});
		FloatPanel=new FloatPanel();
		PS.FloatPanel=FloatPanel;
		add(FloatPanel,"width :2048: , height :2048: ");
	}

	public void paint(Graphics g){
		if(MPlayerControl.isPlaying){
			//g.setColor(keyColor);
		}else{
			g.setColor(bgColor);
			Graphics2D g2=(Graphics2D)g;
			Paint p= new GradientPaint(0,0, bgColor,
					0, getHeight(),new Color(100,100,100));
			//g2.setPaint(p);
		//	g2.fillRoundRect(0, 0, getWidth(), getHeight(),0,0);
		}
		paintComponents(g);
	}



	long getWid(){
		try {
			Class<?> cl;
			cl = Class.forName("sun.awt.windows.WComponentPeer");
			java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
			f.setAccessible(true);
			ComponentPeer peer = canvas.getPeer();
			wid = f.getLong(peer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return wid;
	}

	void checkLayout(){
		new Thread(){
			public void run(){
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(PS.videoFrame!=null){
					SwingUtilities.invokeLater(new Runnable(){

						public void run() {
							PS.videoFrame.validate();
						}

					});

				}
				for(int i=0;i<3;i++){
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					SwingUtilities.invokeLater(new Runnable(){
						public void run() {
							PS.videoFrame.validate();
						}
					});
				}
			}
		}.start();
	}


	public void pack(){
		if(PS.videoFrame.isFullSereen){
			SwingUtilities.invokeLater(new Runnable(){

				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					PS.videoFrame.validate();
					if(PS.videoFrame.VideoFrameRootPanel!=null){
						PS.videoFrame.validate();
						PS.videoFrame.repaint();
						//		System.out.println("KKKKKKKKKK "+System.currentTimeMillis());
					}

				}

			});

		}else{
			Rectangle rec=Tools.getPackOutBound(this,PS.videoRate);
			PS.videoFrame.setBounds(rec);
			checkLayout();
		}

	}

	public void setLocation(Point point){
		super.setLocation(point);

		//	PS.checkFloatPanelLocation();
	}

	public void setBounds(Rectangle rec){
		super.setBounds(rec);
		PS.checkFloatPanelLocation();
	}
}
